var g_gameDataManager = require("g_gameDataManager");
var MessageManager = require("MessageManager");
var prcmd = require("ProtoCmdID")
cc.Class({
    extends: cc.Component,

    properties: {
        headPic:cc.Sprite,
        nameLabel:cc.Label,
        levelLabel:cc.Label,
        coinLabel:cc.Label,
        contentView:cc.Node,
        gameItemTemp:cc.Prefab,
    },

    // use this for initialization
    onLoad: function () {
        this.initView(g_gameDataManager.msgTemp);
        NotificationCenter.on(prcmd.Wait_Room,this.enterWaitRoom,this);
    },

    initView:function(msg)
    {
        var data = msg.data;
        if(data != 'undefined')
        {
            var usrInfo = data.usrInfo;
            if(usrInfo != 'undefined')
            {
                var name = usrInfo.usrName;
                var coin = usrInfo.coin+'';
                var level = usrInfo.level+'';
                var url = usrInfo.headUrl;
                cc.loader.load(url,function (err, texture) {
                    if (err == null) {
                        var frame=new cc.SpriteFrame(texture);
                        this.headPic.spriteFrame = frame;
                    }
                }.bind(this));
                this.nameLabel.string = name;
                this.coinLabel.string = coin;
                this.levelLabel.string = level;
            }
            var gameHallArr = data.gameHall;
            if(gameHallArr != 'undefined')
            {
                var starPos = cc.p(0,-105);
                var disY = -202;
                for(var i=0;i<gameHallArr.length;i++)
                {
                    var obj = gameHallArr[i];
                    var card = cc.instantiate(this.gameItemTemp);
                    card.tag = i;
                    this.contentView.addChild(card);
                    card.position = cc.pAdd(starPos,cc.p(0,disY*i));

                    var eventHandler = new cc.Component.EventHandler();
                    eventHandler.target = this.node;
                    eventHandler.component = this.__proto__.__classname__ ;
                    eventHandler.handler = 'enterGame';
                    var btn = card.getChildByName("startGame");
                    btn.tag = i;
                    btn.getComponent(cc.Button).clickEvents.push(eventHandler);

                    var gameItem = card.getComponent(require("GameItem"));
                    gameItem.nameLabel.string = obj.name;
                    gameItem.onlineLabel.string = obj.onlineNum+'';
                }
            }
        }
    },

    enterGame:function(event)
    {
        var tag = event.target.tag;
        if(tag == 0)
        {
            console.log("enter minesweep");
            MessageManager.enterMineSweep();
        }
    },

    enterWaitRoom:function(eventObj)
    {
        var msg = eventObj.msg;
        g_gameDataManager.msgTemp = msg;
        cc.director.loadScene('RoomScene');
    },

    onDestroy:function()
    {
        NotificationCenter.off(prcmd.Wait_Room,this.enterWaitRoom,this);
    },

});
